﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using siqi.Interfaces;

namespace siqi.Logic.Dispatch
{
    public class LoginJob:IJob
    {
        public Protocol.Login Login
        {
            get;
            set;
        }
        public ISiqiServer SiqiServer
        {
            get;
            set;
        }

        public EC.ISession Session { get;set; }

        public void Execute()
        {
            Protocol.LoginResponse response = new Protocol.LoginResponse();
            response.Success = false;
            try
            {
                Interfaces.Data.Model.User user = SiqiServer.LoginHandler.Login(Login.EMail, Login.Password);
                if (user != null)
                {
                    UserAgent agent = new UserAgent(user, Session);
                    Session[SESSION_KEY.USER] = agent;
                    SiqiServer.AddUser(agent);
                    response.Success = true;
                }
                else
                {
                    response.Message = "用户名和密码不正确!";
                }
            }
            catch (Exception e_)
            {
                response.Message = e_.Message;
            }
            SiqiServer.Send(response, Session);
        }
    }
}
